package com.bdc.user.action;

import com.bdc.user.UserVar;
import com.bdc.user.domain.UserInfo;
import com.bdc.user.service.IUserService;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.opensymphony.xwork2.Action;
import org.apache.log4j.Logger;

/**
 * User: shawn
 * Date: 2011-6-12
 * Time: 21:52:45
 */
public class RegisterAction {

    private Logger logger = Logger.getLogger(RegisterAction.class.getName());

    @Inject
    private IUserService userService;

    private UserInfo userInfo;
    private String passwordRetype;

    private String result;

    @Transactional
    public String register() throws Exception {
        logger.debug("register action start");

        if (!userInfo.getPassword().equals(passwordRetype)) {
            result = "password";
            return Action.SUCCESS;
        }

        long userId = userService.userRegister(userInfo.getMail(), userInfo.getPassword());
        boolean isUserExist = userId == UserVar.REGISTER_ERROR_USER_EXIST;
        if (isUserExist) {
            result = "user";
        }

        return Action.SUCCESS;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public String getPasswordRetype() {
        return passwordRetype;
    }

    public void setPasswordRetype(String passwordRetype) {
        this.passwordRetype = passwordRetype;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}
